package com.tools.s7.utils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static String onTimeStamp(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        return String.valueOf(ts);
    }

    public static String timeStampDate(String seconds) {
        String format = "yyyy.MM.dd";
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds)));
    }

    public static String timeStampMin(String seconds) {
        String format = "yyyy-MM-dd HH:mm";
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds)));
    }

    public static String timeStampSec(String seconds) {
        String format = "yyyy-MM-dd HH:mm:ss";
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds)));
    }

    public static String getTimeName() {
        SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        return formatter.format(curDate);
    }

    public static String getDateName() {
        SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMdd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        return formatter.format(curDate);
    }

    public static String beforeOrAfterNumberDay(String day, int num) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(day, pos);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_YEAR, num);
        return formatter.format(c.getTime());
    }

    public static int getYear() {
//        Calendar c = Calendar.getInstance();
//        return c.get(Calendar.YEAR);
        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        return Integer.parseInt(formatter.format(curDate));
    }

    public static int getMonth() {
//        Calendar c = Calendar.getInstance();
//        return c.get(Calendar.MONTH) + 1;
        SimpleDateFormat formatter = new SimpleDateFormat ("MM");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        return Integer.parseInt(formatter.format(curDate));
    }

    public static int getDay() {
//        Calendar c = Calendar.getInstance();
//        return c.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat formatter = new SimpleDateFormat ("dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        return Integer.parseInt(formatter.format(curDate));
    }

    public static int getWeek() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.DAY_OF_WEEK);
    }

    public static int getWeek(int year, int month, int day){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, day);
        int week = c.get(Calendar.DAY_OF_WEEK);
        return week;
    }

    public static int MaxDayFromDay_OF_MONTH(int year, int month){
        Calendar time = Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR, year);
        time.set(Calendar.MONTH, month - 1);// 注意,Calendar对象默认一月为 0
        int day = time.getActualMaximum(Calendar.DAY_OF_MONTH); // 本月份的天数
        return day;
    }

    public static String getTodayTime() {
        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }

    public static String getTodayTimeAccurate() {
        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }

    public static String getToday() {
        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }

    public static String getTime() {
        SimpleDateFormat formatter = new SimpleDateFormat ("HH:mm");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }

    public static String getTimeAccurate() {
        SimpleDateFormat formatter = new SimpleDateFormat ("HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }

    public static long getTimeMillis(String type, String time) {
        long returnMillis = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        Date d;
        try {
            d = sdf.parse(time);
            returnMillis = d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return returnMillis;
    }

    public static long getTime(String type, String startTime, String endTime) {
        long longExpend = getTimeAccurate(type, startTime, endTime);
        return (longExpend / 60);
    }

    public static long getTimeAccurate(String type, String startTime, String endTime) {
        long longStart = getTimeMillis(type, startTime);
        long longEnd = getTimeMillis(type, endTime);
        long longExpend = longEnd - longStart;
        return (longExpend / 1000);
    }

    public static boolean compareTime(String time1, String time2) {
        return compareTime(time1, time2, ":");
    }

    public static boolean compareTime(String time1, String time2, String sign) {
        String[] time1s = time1.split(sign);
        String[] time2s = time2.split(sign);
        int size = time1s.length > time2s.length ? time1s.length : time2s.length;
        for (int i=0; i<size; i++) {
            int t1 = Integer.valueOf(time1s[i]);
            int t2 = Integer.valueOf(time2s[i]);
            if (t1 > t2) {
                return false;
            } else if (t1 < t2) {
                return true;
            } else {
                if (i == size -1) {
                    return true;
                }
            }
        }
        return false;
    }
}
